package com.sphd.beautysalon.activitys.marking;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

import com.rengwuxian.materialedittext.MaterialEditText;
import com.sphd.beautysalon.base.BaseActivity;
import com.sphd.beautysalon.base.RestaurantUrl;
import com.sphd.beautysalon.info.LoginInfo;
import com.sphd.beautysalon.info.RestaurantInfo;
import com.sphd.beautysalon.presenter.AddCouponPresenterImpl;
import com.sphd.beautysalon.utils.Constant;
import com.sphd.beautysalon.view.AddCouponView;
import com.sphd.beautysalon.R;
import com.wzh.frame.utils.StringUtils;

import org.litepal.crud.DataSupport;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 添加促销信息
 */
public class AddCouponActivity extends BaseActivity implements AddCouponView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.coupon_starttime_textview)
    TextView couponStarttimeTextview;
    @BindView(R.id.coupon_endtime_textview)
    TextView couponEndtimeTextview;
    @BindView(R.id.add_coupon_input)
    MaterialEditText addCouponInput;
    @BindView(R.id.add_coupon_update)
    Button addCouponUpdate;
    //定义显示时间控件
    private Calendar calendar; //通过Calendar获取系统时间
    private int mYear;
    private int mMonth;
    private int mDay;
    private String info;
    private final String time = "年.月.日";

    private RestaurantInfo restaurantInfo;
    private LoginInfo loginInfo;
    Intent intent;
    Bundle bundle;
    AddCouponPresenterImpl addpresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_coupon);
        ButterKnife.bind(this);
        intView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(view -> {
            setResult(0);
            finish();
        });
    }

    private void intView() {
        intent = getIntent();
        bundle = intent.getExtras();
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        loginInfo = DataSupport.findLast(LoginInfo.class);
        calendar = Calendar.getInstance();
        setTitleBar(toolbar, "添加促销信息");
        addpresenter = new AddCouponPresenterImpl(this);
    }

    @OnClick({R.id.coupon_starttime_textview, R.id.coupon_endtime_textview, R.id.add_coupon_update})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.coupon_starttime_textview:
                setTimeDialog(couponStarttimeTextview);
                break;
            case R.id.coupon_endtime_textview:
                setTimeDialog(couponEndtimeTextview);
                break;
            case R.id.add_coupon_update:

                if (isCg_has_storemanager(restaurantInfo) && isSuperManager(loginInfo)) {
                    toast("店长会去操作，您不用费心了");
                } else {
                    if (getMessage() && restaurantInfo.getId() > 0) {
                        addpresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.Addcoupon + "cgid=" + restaurantInfo.getId() +
                                "&info=" + StringUtils.utf8Encode(info));

                    }
                }
                break;
        }
    }

    public boolean getMessage() {
        boolean message = false;
        if (!time.equals(couponStarttimeTextview.getText().toString().trim())
                && !time.equals(couponEndtimeTextview.getText().toString().trim())) {
            message = true;
        } else {
            Toast.makeText(context, "时间输入不能为空", Toast.LENGTH_SHORT).show();
            message = false;
            return message;
        }
        if (!TextUtils.isEmpty(addCouponInput.getText())) {
            message = true;
        } else {
            Toast.makeText(context, "内容输入不能为空", Toast.LENGTH_SHORT).show();
            message = false;
            return message;
        }
        info = addCouponInput.getText().toString().trim() + "(" + couponStarttimeTextview.getText().toString().trim() +
                "至" + couponEndtimeTextview.getText().toString().trim() + ")";
        return message;
    }

    private void setTimeDialog(TextView tv) {
        new DatePickerDialog(AddCouponActivity.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int day) {
                // TODO Auto-generated method stub
                mYear = year;
                mMonth = month;
                mDay = day;
                //更新EditText控件日期 小于10加0
                tv.setText(new StringBuilder().append(mYear).append("-")
                        .append((mMonth + 1) < 10 ? 0 + (mMonth + 1) : (mMonth + 1))
                        .append("-")
                        .append((mDay < 10) ? 0 + mDay : mDay));
            }
        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH)).show();
    }

    @Override
    public void recRes(int res) {
        if (res == 1) {
            toast("添加成功");
            setResult(0);
            finish();
        } else {
            toast("添加失败，请重试！");
        }
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
    }
}
